var dailyReportAjax = function() {
	$.ajax({
		type : 'GET',
		// url地址 应该是servelet地址
		url : '../DailySentimentServlet?method=getTodayReport',
		data : {
		// 假设表名为dailypublish
		// "tablename": "dailypublish",
		// "user":"admin",
		// "pagenum":1
		},
		success : function(data) {
			console.log(data);
			if (data == "" || data == null) {
				var infomation = $("<h4>" + "请先生成今日报告！" + "</h4>");
				$("#dailypublishTable").append(infomation);
			} else {
				var DailySentiment = JSON.parse(data).reports;
				console.log(DailySentiment);
				// var usersData = JSON.parse(data).users;
				$.each(DailySentiment, function(indexs, values) {
					// console.log(this.username);
					var id = this._id;
					console.log(id);
					var val = id.$oid;
					console.log(val);
					var tr = "<tr><td>" + this.title + "</td><td>"
							+ summary(this.text) + "</td><td class=\"center\">"
							+ "<button class=\"btn btn-success\" id=\"" + val
							+ "_checkout\">" + "查看编辑" + "</button>";
					
					tr += selectStatusCheck(val, this.status);
					tr += "</td></tr>";
					// 遍历添加行
					$("#dailypublishTable").append(tr);
					// 绑定查看按钮点击事件
					$("#" + val + "_checkout").click(function() {
						console.log(val);
						viewDetail(val);
					})
					// 绑定选择按钮点击事件
					$("#" + val + "_selectbtn").click(function() {
						console.log(val);
						statusAjax(val);
					});
				});
				// pageMaker(recordsTotal);
			}
		},
		error : function() {
			console.log("error");
		}
	});
}

var createReportAjax = function() {
	$.ajax({
		type : 'GET',
		// url地址 应该是servelet地址
		url : '../DailySentimentServlet?method=creatDailySentiment',
		data : {
		// 假设表名为dailypublish
		// "tablename": "dailypublish",
		// "user":"admin",
		// "pagenum":1
		},
		success : function(data) {
			var DailySentiment = JSON.parse(data).reports;
			// var usersData = JSON.parse(data).users;
			$.each(DailySentiment, function(indexs, values) {
				// console.log(this.username);
				var id = this._id.$oid;
				var tr = "<tr><td>" + this.title + "</td><td>"
						+ summary(this.text) + "</td><td class=\"center\">"
						+ "<button class=\"btn btn-success\" id=\"" + id
						+ "_checkout\">" + "查看编辑" + "</button>";

				tr += selectStatusCheck(id, this.status);
				tr += "</td></tr>";
				// 遍历添加行
				$("#dailypublishTable").append(tr);
				// 绑定查看按钮点击事件
				$("#" + id + "_checkout").click(function() {
					console.log(id);
					viewDetail(id);
				})
				// 绑定选择按钮点击事件
				$("#" + id + "_selectbtn").click(function() {
					console.log(id);
					statusAjax(id);
				});

			});
			// pageMaker(recordsTotal);

		},
		error : function() {
			console.log("error");
		}
	});
}
$(document).ready(function() {
	dailyReportAjax();
	$("#generateReprot").click(createReportAjax);
});

// 简单方法提炼出文章概要，取前100个字
function summary(text) {
	var str = "";
	var i = 0;
	while (str.length <= 100) {
		str += text[i++];
	}
	console.log(str);
	return str;
}
function selectStatusCheck(id, status) {
	if (status == true) {
		console.log("execute here");
		return "<button class=\"btn btn-primary\"id=\"" + id
				+ "_selectbtn\">已发布</button><br>";
	} else {
		return "<button class=\"btn btn-danger\"id=\"" + id
				+ "_selectbtn\">未发布</button>";
	}
}

function statusAjax(id) {
	$.ajax({
		type : "get",
		url : '../DailySentimentServlet?method=changeReportStatus',
		data : {
			"id" : id,
		},
		success : function(data) {
			console.log(data);

			if (data == "false") {
				$("#" + id + "_selectbtn").removeClass("btn-primary").addClass(
						"btn-danger").html("未发布");
			} else {
				$("#" + id + "_selectbtn").removeClass("btn-danger").addClass(
						"btn-primary").html("已发布");
			}
		},
		error : function() {
			console.log("ajax transport error");
		}
	});
}
// 查看编辑按钮方法
function viewDetail(id) {
	var url = "../WebManagement/dailypublishEdit.jsp";
	location.href = url + "?id=" + id;
}

/*
 * $("#dailypublishTable").DataTable({ // 指定要从哪个URL获取数据
 * "sAjaxSource":"../../Detection1.1/UserInfoServlet?method=GetUserList",
 * "sServerMethod":"GET", // 用于指定一屏显示的条数 "iDisplayLength" : 10, //
 * "bJQueryUI":true, // 是否显示（使用）分页器 "bPaginate":true, "pagingType":
 * "simple_numbers", // "dom": '<"top"i>rt<"bottom"flp><"clear">', "dom": 'B<"clear">lfrtip',
 * "buttons": { "name": 'primary', "buttons": [ 'copy', 'csv', 'excel' ] }, //
 * 指定当从服务端获取表格数据时，数据项使用的名字 default 'aaData' "sAjaxDataProp":"users",
 * "aoColumns": [ {"mDataProp":"user_id","bVisible":false},
 * {"mDataProp":"username"}, {"mDataProp":"registered_date"},
 * {"mDataProp":"role"}, {"mDataProp":"status"} ], // 没有数据时显示 如下内容
 * "sZeroRecords": "No records to display", // 以指定当正在处理数据的时候，是否显示“正在处理”这个提示信息
 * "bprocessing": true, "bserverSide": true, //
 * 用于指明当执行dataTable绑定时，是否返回DataTable对象 "bRetrieve":true, //
 * 用于当要在同一个元素上执行新的dataTable绑定时，将之前的那个数据对象清除掉，换以新的对象设置 "bDestroy": true,
 * "buttons": [ { extend: '查看', editor: myEditor }, { extend: 'edit', editor:
 * myEditor }, { extend: 'remove', editor: myEditor } ], "buttons": [ { extend:
 * 'selectedSingle', text: 'Log selected data', action: function ( e, dt,
 * button, config ) { console.log( dt.row( { selected: true } ).data() ); } } ],
 * "buttons": [ 'copy', 'csv', 'excel' ], //多语言配置 "oLanguage": { "sProcessing":
 * "正在加载中......", "sLengthMenu": "每页显示 _MENU_ 条记录", "sZeroRecords":
 * "对不起，查询不到相关数据！", "sEmptyTable": "表中无数据存在！", "sInfo": "当前显示 _START_ 到 _END_
 * 条，共 _TOTAL_ 条记录", "sInfoFiltered": "数据表中共为 _MAX_ 条记录", "sSearch": "搜索",
 * "oPaginate": { "sFirst": "首页", "sPrevious": "上一页", "sNext": "下一页", "sLast":
 * "末页" } }, // 是否让各列具有按列排序功能 bSort "columns": [ { "users": "user_id"}, {
 * "users": "username","bSort":true}, { "users": "registered_date" }, { "users":
 * "role" }, { "users": "status" } ], "columnDefs": [ { "targets": [3], "data":
 * "status", "render": function(data, type, full) { // return "<a
 * href='/view?id=" + data + "'>Update</a>"; return "<span
 * style='color:red;'>"+data+"</span>"; } } ] })
 */